package com.ssy.lingxi.member.merchant.api.handler.validator;

import com.ssy.lingxi.member.merchant.api.handler.annotation.MemberRightSpendTypeAnno;
import com.ssy.lingxi.member.merchant.api.model.constant.MemberRightSpendTypeEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 权益消费类型校验注解实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-11-20
 */
public class MemberRightSpendTypeValidator implements ConstraintValidator<MemberRightSpendTypeAnno, Integer> {
    private boolean required = false;

    @Override
    public void initialize(MemberRightSpendTypeAnno constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(!required) {
            return true;
        }

        if(value == null) {
            return false;
        }

        return Arrays.stream(MemberRightSpendTypeEnum.values()).anyMatch(e -> e.getTypeEnum().equals(value));
    }
}
